package org.framework.edge.builder.infrastructure.persistence;

import org.framework.edge.builder.infrastructure.entity.DockerImagesDO;
import org.framework.edge.builder.infrastructure.converter.DockerImagesConverter;
import org.framework.edge.builder.infrastructure.mapper.DockerImagesMapper;
import org.framework.edge.builder.domain.model.docker.images.DockerImagesRepository;
import org.springframework.stereotype.Repository;

import java.util.stream.Collectors;

import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazyWrappers;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;
import jakarta.annotation.Resource;
import org.framework.edge.builder.domain.model.docker.images.DockerImages;
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;

import java.util.List;

import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;

/**
 * describe docker镜像
 *
 * @author Jia wei Wu
 * @date 2025/07/10 05:16 下午
 * @see org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyInfrastructurePersistence
 **/
@Repository
public class DockerImagesRepositoryImpl implements DockerImagesRepository {

    @Resource
    LazyLambdaStream lazyLambdaStream;

    /**
     * describe 新增docker镜像
     *
     * @param dockerImages 新增docker镜像
     * @return {@link Result<DockerImages>} docker镜像新增后领域对象
     * @author Jia wei Wu
     * @date 2025/07/10 05:16 下午
     **/

    @Override
    public Result<DockerImages> story(DockerImages dockerImages) {
        DockerImagesDO dockerImagesDO = DockerImagesConverter.INSTANCE.fromDockerImages(dockerImages);
        lazyLambdaStream.upsert(dockerImagesDO);
        return ResultFactory.successOf();
    }

    /**
     * describe 批量新增docker镜像
     *
     * @param dockerImagesList 批量新增docker镜像
     * @return {@link Result<List<DockerImages>>} docker镜像新增后领域对象集合
     * @author Jia wei Wu
     * @date 2025/07/10 05:16 下午
     **/

    @Override
    public Result<List<DockerImages>> batchStory(List<DockerImages> dockerImagesList) {
        List<DockerImagesDO> dockerImagesDOList = dockerImagesList.stream().map(DockerImagesConverter.INSTANCE::fromDockerImages).collect(Collectors.toList());
        lazyLambdaStream.upsert(dockerImagesDOList);
        return ResultFactory.successOf();
    }

    /**
     * describe 查询单个docker镜像
     *
     * @param dockerImages 查询单个docker镜像
     * @return {@link Result<DockerImages>} docker镜像领域对象
     * @author Jia wei Wu
     * @date 2025/07/10 05:16 下午
     **/

    @Override
    public Result<DockerImages> findOne(DockerImages dockerImages) {
        DockerImagesDO dockerImagesDO = DockerImagesConverter.INSTANCE.fromDockerImages(dockerImages);
        DockerImages dockerImagesOne = lazyLambdaStream.selectOne(
                LazyWrappers.<DockerImagesDO>lambdaWrapper()
                        .eqIgnoreEmpty(DockerImagesDO::getCreateTime, dockerImagesDO.getCreateTime())
                        .eqIgnoreEmpty(DockerImagesDO::getId, dockerImagesDO.getId())
                        .eqIgnoreEmpty(DockerImagesDO::getIsDeleted, false)
                        .eqIgnoreEmpty(DockerImagesDO::getName, dockerImagesDO.getName())
                        .eqIgnoreEmpty(DockerImagesDO::getRepositoryId, dockerImagesDO.getRepositoryId())
                        .eqIgnoreEmpty(DockerImagesDO::getUpdateTime, dockerImagesDO.getUpdateTime())
                        .eqIgnoreEmpty(DockerImagesDO::getVersion, dockerImagesDO.getVersion())
                , DockerImages.class);
        return ResultFactory.successOf(dockerImagesOne);
    }

    /**
     * describe 查询多个docker镜像
     *
     * @param dockerImages 查询多个docker镜像
     * @return {@link Result<List<DockerImages>>} docker镜像领域对象
     * @author Jia wei Wu
     * @date 2025/07/10 05:16 下午
     **/

    @Override
    public Result<List<DockerImages>> findList(DockerImages dockerImages) {
        DockerImagesDO dockerImagesDO = DockerImagesConverter.INSTANCE.fromDockerImages(dockerImages);
        List<DockerImages> dockerImagesList = lazyLambdaStream.selectList(
                LazyWrappers.<DockerImagesDO>lambdaWrapper()
                        .eqIgnoreEmpty(DockerImagesDO::getCreateTime, dockerImagesDO.getCreateTime())
                        .eqIgnoreEmpty(DockerImagesDO::getId, dockerImagesDO.getId())
                        .eqIgnoreEmpty(DockerImagesDO::getIsDeleted, false)
                        .eqIgnoreEmpty(DockerImagesDO::getName, dockerImagesDO.getName())
                        .eqIgnoreEmpty(DockerImagesDO::getRepositoryId, dockerImagesDO.getRepositoryId())
                        .eqIgnoreEmpty(DockerImagesDO::getUpdateTime, dockerImagesDO.getUpdateTime())
                        .eqIgnoreEmpty(DockerImagesDO::getVersion, dockerImagesDO.getVersion())
                , DockerImages.class);
        return ResultFactory.successOf(dockerImagesList);
    }

    /**
     * describe 分页查询多个docker镜像
     *
     * @param size         当前页数
     * @param current      当前页
     * @param dockerImages 分页查询多个docker镜像
     * @return {@link Result<LazyPage<DockerImages>>} 分页docker镜像领域对象
     * @author Jia wei Wu
     * @date 2025/07/10 05:16 下午
     **/

    @Override
    public Result<LazyPage<DockerImages>> findPage(int size, int current, DockerImages dockerImages) {
        DockerImagesDO dockerImagesDO = DockerImagesConverter.INSTANCE.fromDockerImages(dockerImages);
        LazyPage<DockerImages> lazyPage = new LazyPage<>(current, size);
        LazyPage<DockerImages> dockerImagesLazyPage = lazyLambdaStream.selectPage(
                LazyWrappers.<DockerImagesDO>lambdaWrapper()
                        .eqIgnoreEmpty(DockerImagesDO::getCreateTime, dockerImagesDO.getCreateTime())
                        .eqIgnoreEmpty(DockerImagesDO::getId, dockerImagesDO.getId())
                        .eqIgnoreEmpty(DockerImagesDO::getIsDeleted, false)
                        .eqIgnoreEmpty(DockerImagesDO::getName, dockerImagesDO.getName())
                        .eqIgnoreEmpty(DockerImagesDO::getRepositoryId, dockerImagesDO.getRepositoryId())
                        .eqIgnoreEmpty(DockerImagesDO::getUpdateTime, dockerImagesDO.getUpdateTime())
                        .eqIgnoreEmpty(DockerImagesDO::getVersion, dockerImagesDO.getVersion())
                , lazyPage, DockerImages.class);
        return ResultFactory.successOf(dockerImagesLazyPage);
    }

    /**
     * describe 删除docker镜像
     *
     * @param dockerImages 删除docker镜像
     * @return {@link Result<DockerImages>} docker镜像
     * @author Jia wei Wu
     * @date 2025/07/10 05:16 下午
     **/

    @Override
    public Result<DockerImages> remove(DockerImages dockerImages) {
        DockerImagesDO dockerImagesDO = DockerImagesConverter.INSTANCE.fromDockerImages(dockerImages);
        lazyLambdaStream.delete(
                LazyWrappers.<DockerImagesDO>lambdaWrapper()
                        .eqIgnoreEmpty(DockerImagesDO::getCreateTime, dockerImagesDO.getCreateTime())
                        .eqIgnoreEmpty(DockerImagesDO::getId, dockerImagesDO.getId())
                        .eqIgnoreEmpty(DockerImagesDO::getIsDeleted, false)
                        .eqIgnoreEmpty(DockerImagesDO::getName, dockerImagesDO.getName())
                        .eqIgnoreEmpty(DockerImagesDO::getRepositoryId, dockerImagesDO.getRepositoryId())
                        .eqIgnoreEmpty(DockerImagesDO::getUpdateTime, dockerImagesDO.getUpdateTime())
                        .eqIgnoreEmpty(DockerImagesDO::getVersion, dockerImagesDO.getVersion())
        );
        return ResultFactory.successOf();
    }

    /**
     * describe 是否存在docker镜像
     *
     * @param dockerImages docker镜像领域对象
     * @return {@link Result<Boolean>} 是否存在 true 存在，false 不存在
     * @author Jia wei Wu
     * @date 2025/07/10 05:16 下午
     **/

    @Override
    public Result<Boolean> exists(DockerImages dockerImages) {
        DockerImagesDO dockerImagesDO = DockerImagesConverter.INSTANCE.fromDockerImages(dockerImages);
        Boolean exists = lazyLambdaStream.exists(
                LazyWrappers.<DockerImagesDO>lambdaWrapper()
                        .eqIgnoreEmpty(DockerImagesDO::getCreateTime, dockerImagesDO.getCreateTime())
                        .eqIgnoreEmpty(DockerImagesDO::getId, dockerImagesDO.getId())
                        .eqIgnoreEmpty(DockerImagesDO::getIsDeleted, false)
                        .eqIgnoreEmpty(DockerImagesDO::getName, dockerImagesDO.getName())
                        .eqIgnoreEmpty(DockerImagesDO::getRepositoryId, dockerImagesDO.getRepositoryId())
                        .eqIgnoreEmpty(DockerImagesDO::getUpdateTime, dockerImagesDO.getUpdateTime())
                        .eqIgnoreEmpty(DockerImagesDO::getVersion, dockerImagesDO.getVersion())
        );
        return ResultFactory.successOf(exists);
    }

}